package com.james.customcamera.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.james.customcamera.R;
import com.james.customcamera.base.BaseActivity;
import com.james.customcamera.contract.LoginContract;
import com.james.customcamera.presenter.LoginPresenter;
import com.james.customcamera.ui.activity.MainActivity;
import com.james.customcamera.ui.activity.RegisterActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity<LoginPresenter> implements LoginContract.LoginView {

    @BindView(R.id.login_username)
    EditText loginUsernameEv;
    @BindView(R.id.login_password)
    EditText loginPasswordEv;
    @BindView(R.id.login_login)
    SuperButton loginLoginBtn;
    @BindView(R.id.login_register)
    TextView loginRegisterTv;
    @BindView(R.id.login_progress)
    ProgressBar loginProgressPb;

    @Override
    protected void initView() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected LoginPresenter loadPresenter() {
        return new LoginPresenter();
    }


    @Override
    protected void initData() {

    }

    @Override
    protected void otherViewClick(View view) {

    }


    @OnClick({R.id.login_login, R.id.login_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.login_login:
                mPresenter.login(getUserName(), getPassword());
                break;
            case R.id.login_register:
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void showLoading() {
        loginProgressPb.setVisibility(View.VISIBLE);
    }

    public void stopLoading() {
        loginProgressPb.setVisibility(View.GONE);
    }

    @Override
    public boolean isActive() {
        return false;
    }

    @Override
    public String getUserName() {
        return loginUsernameEv.getText().toString().trim();
    }

    @Override
    public String getPassword() {
        return loginPasswordEv.getText().toString().trim();
    }

    @Override
    public void LoginSuccess(String successMsg) {
        toast(successMsg);
        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void LoginFail(String failMsg) {
        toast(failMsg);
    }

    public boolean checkNull() {
        boolean isNull = false;
        if (TextUtils.isEmpty(getUserName())) {
            loginUsernameEv.setError("账号不能为空");
            isNull = true;
        } else if (TextUtils.isEmpty(getPassword())) {
            loginPasswordEv.setError("密码不能为空");
            isNull = true;
        }
        return isNull;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
